package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.model.entity.user.AddPackersEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.PackersPublishNextActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import io.reactivex.functions.Consumer;

public class PackersNextModel extends BaseViewModel {
    public ObservableField<AddPackersEntity> addPackersEntity = new ObservableField<>(new AddPackersEntity());
    private PackersPublishNextActivity activity;
    private SelfRepertory repertory;

    public PackersNextModel(PackersPublishNextActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void addPackersModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.addPackers(addPackersEntity.get())
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showSuccess();
                        } else {
                            MToast.showToast(activity, baseEntity.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void editPackersModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.editPackers(addPackersEntity.get())
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showSuccess();
                        } else {
                            MToast.showToast(activity, baseEntity.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }
}
